package com.one.consumer.order.ui;

import android.content.Intent;
import android.view.View;
import android.widget.RatingBar;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.one.common.config.RouterPath;
import com.one.common.manager.imagchoose.ChooseManager;
import com.one.common.manager.imagchoose.IChooseImg;
import com.one.common.utils.list.ListUtils;
import com.one.common.view.photolayout.SortableNinePhotoLayout;
import com.one.common.view.preview.PreviewPicActivity;
import com.one.common.view.widget.BigEditView;
import com.one.common.view.widget.MyTitleBar;
import com.one.consumer.BaseActivityC;
import com.one.consumer.R;
import com.one.consumer.order.presenter.AddCommentPresenter;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/11/3 8:26 PM
 * description ： 添加评论页面
 **/
@Route(path = RouterPath.ADD_COMMENT)
public class AddCommentActivity extends BaseActivityC<AddCommentPresenter> implements SortableNinePhotoLayout.Delegate {
    @BindView(R.id.rb_star_1)
    RatingBar rbStar1;
    @BindView(R.id.tv_score_1)
    TextView tvScore1;
    @BindView(R.id.rb_star_2)
    RatingBar rbStar2;
    @BindView(R.id.tv_score_2)
    TextView tvScore2;
    @BindView(R.id.be_dsc)
    BigEditView beDsc;
    @BindView(R.id.snp_photo_service)
    SortableNinePhotoLayout snpPhotoService;
    @BindView(R.id.tv_submit)
    TextView tvSubmit;

    private String scoer1 = "5", score2 = "5";

    @Override
    protected void initPresenter() {
        mPresenter = new AddCommentPresenter(this, this);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_add_comment;
    }

    @Override
    protected void initTitle() {
        super.initTitle();
        getMyTitleBar().setMode(MyTitleBar.Mode.BACK_TITLE)
                .setTitleText("服务评价");
    }

    @Override
    public void initView() {
        super.initView();
        snpPhotoService.setDelegate(this);
        rbStar1.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
                tvScore1.setText(v + "分");
                scoer1 = v + "";
            }
        });
        rbStar2.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
                tvScore2.setText(v + "分");
                score2 = v + "";
            }
        });
    }

    @OnClick(R.id.tv_submit)
    public void submit() {
        // 添加评价并返回上一个页面
        mPresenter.submit(scoer1,score2, beDsc.getText(),snpPhotoService.getData());
    }


    @Override
    public void onClickAddNinePhotoItem(SortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, ArrayList<String> models) {
        ChooseManager.getChooser().chooseSingle(this);
    }

    @Override
    public void onClickDeleteNinePhotoItem(SortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, String model, ArrayList<String> models) {
        snpPhotoService.removeItem(position);
    }

    @Override
    public void onClickNinePhotoItem(SortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, String model, ArrayList<String> models) {
        PreviewPicActivity.starPreview(this, sortableNinePhotoLayout.getData(), position);
    }

    @Override
    public void onNinePhotoItemExchanged(SortableNinePhotoLayout sortableNinePhotoLayout, int fromPosition, int toPosition, ArrayList<String> models) {

    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null) {
            if (resultCode == RESULT_OK) {
                // 选择图片的结果
                ArrayList<String> pathList =
                        data.getStringArrayListExtra(IChooseImg.EXTRA_RESULT_SELECTION);
                if (ListUtils.isNotEmpty(pathList)) {
                    snpPhotoService.addLastItem(pathList.get(0));
                }

            }
        }
    }
}